package com.itqf.service.impl;

import com.itqf.bean.DtsRole;
import com.itqf.bean.DtsRoleExample;
import com.itqf.mapper.DtsRoleMapper;
import com.itqf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/17 11:00
 * description:
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    private DtsRoleMapper dtsRoleMapper;

    /**
     * 根据角色id查询角色名称！
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<String> findRoleNameByRoleIds(Integer[] roleIds) {

        //1.查询数据
        DtsRoleExample example = new DtsRoleExample();
        DtsRoleExample.Criteria criteria = example.createCriteria();

        /**
         * id in  enabled = 1  deleted 0
         */

        criteria.andIdIn(Arrays.asList(roleIds));

        criteria.andEnabledEqualTo(true);

        criteria.andDeletedEqualTo(false);

        List<DtsRole> roleList =   dtsRoleMapper.selectByExample(example);

        if (roleList == null || roleList.size() == 0) {

            return null;
        }

        //2.返回结果
        List<String> roleNames = new ArrayList<>();

        for (DtsRole dtsRole : roleList) {
            roleNames.add(dtsRole.getName());
        }

        return roleNames;
    }

    /**
     * 是不是管理员
     * 判断依据： 角色命中包含管理员
     *
     * @param roleIds
     * @return
     */
    @Override
    public boolean isAdminRole(Integer[] roleIds) {


        List<String> roleNames = findRoleNameByRoleIds(roleIds);

        if (roleNames == null || roleNames.size() == 0) {

            return false;
        }


        for (String roleName : roleNames) {

            if (roleName.contains("管理员")) {

                return true;
            }
        }

        return false;
    }

    /**
     * 查询全部role数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findRoleAll() {

        //1.查询全部数据
        //查询条件传入空证明是查询全部数据
        List<DtsRole> dtsRoles = dtsRoleMapper.selectByExample(null);

        //2.封装数据

        List<Map<String,Object>> data  = new ArrayList<>();
        for (DtsRole dtsRole : dtsRoles) {
            Map<String,Object> map = new HashMap<>();
            map.put("value", dtsRole.getId());
            map.put("label", dtsRole.getName());
            data.add(map);
        }

        return data;
    }


}
